package com.wzq.solution;

class Solution31 {
    public static void nextPermutation(int[] nums) {
        int len = nums.length;
        int maxIndex = len-1;
        for(int i=len-2;i>=0; i--){
           if(nums[i] < nums[maxIndex]){
               for(int j=len-1;j>i;j--){
                   if(nums[j]>nums[i]){
                       swap(nums,i,j);
                       reverse(nums,i+1,len-1);
                       break;
                   }
               }
               break;
           }else{
               maxIndex = i;
           }
        }
        if(maxIndex == 0){
            reverse(nums,0,len-1);
        }
        System.out.println();
    }
    
    public static void swap(int[] arr,int i,int j){
        arr[i] = arr[i]^arr[j];
        arr[j] = arr[i]^arr[j];
        arr[i] = arr[i]^arr[j];
    }
    
    
    public static void reverse(int arr[],int i,int j){
        while(i<j){
            swap(arr,i++,j--);
        }
    }

    public static void main(String[] args) {
        nextPermutation(new int[]{2});
    }
}